package com.atguigu.szdz.dcsyl.controller;

import com.alibaba.excel.EasyExcel;
import com.atguigu.szdz.dcsyl.common.listener.WisListener;
import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.entity.WisDataEntity;
import com.atguigu.szdz.dcsyl.service.YlDataService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wisData")
public class WisDataController {
    @Autowired
    private YlDataService ylDataService;
    @Autowired
    private MapperUtils mapperUtils;

    /**
     * 获取 wis 数据
     * @param map
     * @return
     */
    @PostMapping("/getWisData")
    public R getWisData(@RequestBody Map<String, Integer> map) {
        //获取井ID
        Integer wellId = map.get("wellId");
        //获取当前页
        Integer currentPage = map.get("currentPage");
        //获取每页显示条数
        Integer pageSize = map.get("pageSize");
        //获取总条数
        String sql = "select count(*) from dc_wisdata where well_id=" + wellId;
        List<Map<String, Object>> contList = mapperUtils.queryBySql(sql);
        Integer total = Integer.parseInt(contList.get(0).get("count").toString());
        //总页数
        Integer totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        //获取数据
        List<Map<String, Object>> qtListData = ylDataService.getWisData(wellId, currentPage, pageSize);
        return R.ok().put("wisListData", qtListData).put("total", total).put("totalPage", totalPage);
    }

    /**
     * 导入wis数据的文件
     * @param file
     * @param wellId
     * @return
     */
    @Operation(summary = "解析 wis 数据的excel文件")
    @PostMapping("/importFile")
    public R importWisData(@RequestParam("file") MultipartFile file, @RequestParam("well_id") Integer wellId) {
        //先去数据库查询是否有该井
        String sql = "select * from dc_well_info where id=" + wellId;
        List<Map<String, Object>> wellList = mapperUtils.queryBySql(sql);
        if (wellList.size() == 0) {
            return R.error("该井不存在");
        }
        List<WisDataEntity> list = new ArrayList<>();
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls") && !fileName.endsWith(".csv"))) {
            return R.error("文件格式错误");
        }
        try (InputStream inputStream = file.getInputStream()){
            WisListener wisListener = new WisListener();
            //读取excel文件
            EasyExcel.read(inputStream, WisDataEntity.class,wisListener).excelType(com.alibaba.excel.support.ExcelTypeEnum.CSV).headRowNumber(2).sheet().doRead();
            //获取读取到的数据
            list = wisListener.getList();
            //返回成功
            return R.ok("解析成功").put("wisListData", list);
        }catch (Exception e) {
            e.printStackTrace();
            return R.error("文件导入失败");
        }
    }
    /**
     * 导入wis数据
     */
    @Operation(summary = "导入wis数据")
    @PostMapping("/importWisData")
    public R importWisData(@RequestBody Map<String, Object> map) {
        //获取井ID
        Integer wellId = Integer.parseInt(map.get("wellId").toString());
        //获取 wis 数据列表
        List<WisDataEntity> list = new ArrayList<>();
        List<Map<String,Object>> wisList = (List<Map<String, Object>>) map.get("wisListData");
        //将 wisList 转换为 WisDataEntity 列表
        for (Map<String, Object> wisMap : wisList) {
            WisDataEntity wisDataEntity = new WisDataEntity();
            wisDataEntity.setMd_ref(wisMap.get("md_ref") != null ? Double.parseDouble(wisMap.get("md_ref").toString()) : null);
            wisDataEntity.setBddi(wisMap.get("bddi") != null ? Double.parseDouble(wisMap.get("bddi").toString()) : null);
            wisDataEntity.setBdti(wisMap.get("bdti") != null ? Double.parseDouble(wisMap.get("bdti").toString()) : null);
            wisDataEntity.setBrvc(wisMap.get("brvc") != null ? Double.parseDouble(wisMap.get("brvc").toString()) : null);
            wisDataEntity.setC1(wisMap.get("c1") != null ? Double.parseDouble(wisMap.get("c1").toString()) : null);
            wisDataEntity.setC2(wisMap.get("c2") != null ? Double.parseDouble(wisMap.get("c2").toString()) : null);
            wisDataEntity.setC02a(wisMap.get("c02a") != null ? Double.parseDouble(wisMap.get("c02a").toString()) : null);
            wisDataEntity.setC3(wisMap.get("c3") != null ? Double.parseDouble(wisMap.get("c3").toString()) : null);
            wisDataEntity.setCo2(wisMap.get("co2") != null ? Double.parseDouble(wisMap.get("co2").toString()) : null);
            wisDataEntity.setDbtm(wisMap.get("dbtm") != null ? Double.parseDouble(wisMap.get("dbtm").toString()) : null);
            wisDataEntity.setDmea(wisMap.get("dmea") != null ? Double.parseDouble(wisMap.get("dmea").toString()) : null);
            wisDataEntity.setDoc(wisMap.get("doc") != null ? Double.parseDouble(wisMap.get("doc").toString()) : null);
            wisDataEntity.setDrtm(wisMap.get("drtm") != null ? Double.parseDouble(wisMap.get("drtm").toString()) : null);
            wisDataEntity.setDver(wisMap.get("dver") != null ? Double.parseDouble(wisMap.get("dver").toString()) : null);
            wisDataEntity.setEcdt(wisMap.get("ecdt") != null ? Double.parseDouble(wisMap.get("ecdt").toString()) : null);
            wisDataEntity.setEtha(wisMap.get("etha") != null ? Double.parseDouble(wisMap.get("etha").toString()) : null);
            wisDataEntity.setGr(wisMap.get("gr") != null ? Double.parseDouble(wisMap.get("gr").toString()) : null);
            wisDataEntity.setHkla(wisMap.get("hkla") != null ? Double.parseDouble(wisMap.get("hkla").toString()) : null);
            wisDataEntity.setIbta(wisMap.get("ibta") != null ? Double.parseDouble(wisMap.get("ibta").toString()) : null);
            wisDataEntity.setIld(wisMap.get("ild") != null ? Double.parseDouble(wisMap.get("ild").toString()) : null);
            wisDataEntity.setIpna(wisMap.get("ipna") != null ? Double.parseDouble(wisMap.get("ipna").toString()) : null);
            wisDataEntity.setMdia(wisMap.get("mdia") != null ? Double.parseDouble(wisMap.get("mdia").toString()) : null);
            wisDataEntity.setMfia(wisMap.get("mfia") != null ? Double.parseDouble(wisMap.get("mfia").toString()) : null);
            wisDataEntity.setMfoa(wisMap.get("mfoa") != null ? Double.parseDouble(wisMap.get("mfoa").toString()) : null);
            wisDataEntity.setMfop(wisMap.get("mfop") != null ? Double.parseDouble(wisMap.get("mfop").toString()) : null);
            wisDataEntity.setMse(wisMap.get("mse") != null ? Double.parseDouble(wisMap.get("mse").toString()) : null);
            wisDataEntity.setMtha(wisMap.get("mtha") != null ? Double.parseDouble(wisMap.get("mtha").toString()) : null);
            wisDataEntity.setNbta(wisMap.get("nbta") != null ? Double.parseDouble(wisMap.get("nbta").toString()) : null);
            wisDataEntity.setNpna(wisMap.get("npna") != null ? Double.parseDouble(wisMap.get("npna").toString()) : null);
            wisDataEntity.setPower(wisMap.get("power") != null ? Double.parseDouble(wisMap.get("power").toString()) : null);
            wisDataEntity.setPrpa(wisMap.get("prpa") != null ? Double.parseDouble(wisMap.get("prpa").toString()) : null);
            wisDataEntity.setRopa(wisMap.get("ropa") != null ? Double.parseDouble(wisMap.get("ropa").toString()) : null);
            wisDataEntity.setRpma(wisMap.get("rpma") != null ? Double.parseDouble(wisMap.get("rpma").toString()) : null);
            wisDataEntity.setRt(wisMap.get("rt") != null ? Double.parseDouble(wisMap.get("rt").toString()) : null);
            wisDataEntity.setSppa(wisMap.get("sppa") != null ? Double.parseDouble(wisMap.get("sppa").toString()) : null);
            wisDataEntity.setSpr1(wisMap.get("spr1") != null ? Double.parseDouble(wisMap.get("spr1").toString()) : null);
            wisDataEntity.setTg(wisMap.get("tg") != null ? Double.parseDouble(wisMap.get("tg").toString()) : null);
            wisDataEntity.setTotg(wisMap.get("totg") != null ? Double.parseDouble(wisMap.get("totg").toString()) : null);
            wisDataEntity.setTqa(wisMap.get("tqa") != null ? Double.parseDouble(wisMap.get("tqa").toString()) : null);
            wisDataEntity.setTva(wisMap.get("tva") != null ? Double.parseDouble(wisMap.get("tva").toString()) : null);
            wisDataEntity.setWoba(wisMap.get("woba") != null ? Double.parseDouble(wisMap.get("woba").toString()) : null);
            wisDataEntity.setIC4(wisMap.get("ic4") != null ? Double.parseDouble(wisMap.get("ic4").toString()) : null);
            wisDataEntity.setDTim(wisMap.get("dTim") != null ? Double.parseDouble(wisMap.get("dTim").toString()) : null);
            wisDataEntity.setIC5(wisMap.get("ic5") != null ? Double.parseDouble(wisMap.get("ic5").toString()) : null);
            wisDataEntity.setNC4(wisMap.get("nc4") != null ? Double.parseDouble(wisMap.get("nc4").toString()) : null);
            wisDataEntity.setNC5(wisMap.get("nc5") != null ? Double.parseDouble(wisMap.get("nc5").toString()) : null);
            list.add(wisDataEntity);
        }
        //删除旧数据
        ylDataService.removeWisData(wellId);
        //批量插入数据
        ylDataService.saveBatchWis(list, wellId);
        return R.ok("导入成功");
    }
}
